package com.sm.service.Impl;

import com.sm.dao.*;
import com.sm.entity.AttendanceTotal;
import com.sm.entity.UserInfo;
import com.sm.entity.Wages;
import com.sm.entity.WagesLevel;
import com.sm.service.WagesService;
import com.sm.utils.AttendanceUtil;
import com.sm.utils.ExcelUtil;
import com.sm.utils.WorkTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.java2d.pipe.AAShapePipe;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WagesServiceImpl implements WagesService {

    @Autowired
    UserDao userDao;
    @Autowired
    WagesDao wagesDao;
    @Autowired
    AttendanceDao attendanceDao;
    @Autowired
    UserWelfareDao userWelfareDao;
    @Autowired
    WagesLevelDao wagesLevelDao;

    public void wagesSettlement() throws Exception{
        //结算工资
        //获取考勤总表工作时间  获取工资表中的福利项目  填入应发工资列表
        List<AttendanceTotal> totalList = attendanceDao.findAllATotal(AttendanceUtil.getMonth());
        for (AttendanceTotal attendanceTotal:totalList){
            wagesHelper(attendanceTotal);
        }
        //计算税后收入 填入实发工资列表
        //两表待删除月份置零 员工福利表删除
        Integer delmonth = AttendanceUtil.getDelMonth();
        wagesDao.deleteWages(delmonth);//工资表
        attendanceDao.deleteAttendanceTotal(delmonth);//考勤总表
        attendanceDao.deleteAllAttendance(delmonth);//每日考勤签到表
        userWelfareDao.deleteUserWelfare(delmonth);//员工福利表
    }

    //49 70
    public List<WagesLevel> findwagesLevel() throws Exception {
        return wagesLevelDao.findWagesLevel();
    }

    public UserInfo findOneByUid(Integer uid) throws Exception {
        return userDao.findByUid(uid);
    }

    public List<Wages> findOneWages(String uid, String name)throws Exception {
        List<Wages> wagesList = new ArrayList<Wages>();
//        System.out.println("uid: "+uid+" name: "+name);
        if(name != null){
            List<Integer> uids = userDao.findUidByUsernameOrName(null, name);
            if(uids==null||uids.size()==0)return wagesList;
            for (Integer id :uids){
                //更新工资表
                wagesHelper(attendanceDao.findATotalByUid(id,AttendanceUtil.getMonth()));
                wagesList.add(wagesDao.findWagesByUid(id,AttendanceUtil.getMonth()));
            }
        }else{
            //更新工资表
            wagesHelper(attendanceDao.findATotalByUid(Integer.valueOf(uid),AttendanceUtil.getMonth()));
            wagesList.add(wagesDao.findWagesByUid(Integer.valueOf(uid),AttendanceUtil.getMonth()));
        }
        return wagesList;
    }

    public void updateSalaryOrLevel(UserInfo userInfo) throws Exception {
        userDao.updateSalaryOrLevel(userInfo);
        wagesHelper(attendanceDao.findATotalByUid(userInfo.getUid(),AttendanceUtil.getMonth()));
    }

    public void outExcel() throws Exception {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String pathname = "E:\\ERP公司"+sdf.format(date)+"份工资表.xlsx";
        List<Wages> list = wagesDao.fingAllWages(AttendanceUtil.getMonth());
        ExcelUtil.OutputExcel(pathname,list);

    }

    //计算工资 并将数据填写到工资表中
    private void wagesHelper(AttendanceTotal attendanceTotal) throws Exception {
        Integer uid = attendanceTotal.getUserInfo().getUid();
        Integer month = AttendanceUtil.getMonth();
        Double money = WorkTimeUtil.calculateWages(attendanceTotal);
        //从user表中获取基本工资
        Double baseSalary = attendanceTotal.getUserInfo().getBaseSalary();
        //根据工资级别查询工资
        Double jobSalary = wagesLevelDao.findAmountByLevel(attendanceTotal.getUserInfo().getWagesLevel());
        //从工资表获取福利金额
        Double welfare =  wagesDao.findWelfareByUid(uid,month);
        //计算应发工资
        Double wagesPayable = money+jobSalary+welfare;
        //缺个税计算公式
        Double wageReal = WorkTimeUtil.personalTax(wagesPayable);
        Wages wages = new Wages(uid,baseSalary,jobSalary,wagesPayable,wageReal,month);
        //将基本工资，职务工资 应发工资 实发工资   填写到工资表中
        wagesDao.updateWages(wages);
    }
}
